home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java for 3D & VRML Worlds
/
Java for 3d and VRML Worlds.iso
/
examples
/
chap04
/
FloatingAgent.java
< prev
next >
Wrap
Text File
|
1996-09-19
|
1KB
|
46 lines
//
// an agent is floating randomly.
//
import java.util.*;
import vrml.*;
import vrml.node.*;
import vrml.field.*;
public class FloatingAgent extends Script{
SFVec3f setAgentPosition;
float agentPosition[] = new float[3];
Random randomNumGenerator = new Random();
public void initialize(){
// get the reference of the event-out 'setAgentPosition'.
setAgentPosition = (SFVec3f)getEventOut("setAgentPosition");
// initialize the agent position.
agentPosition[0] = 0.0f;
agentPosition[1] = 0.0f;
agentPosition[2] = 0.0f;
}
public void processEvent(Event e){
if(e.getName().equals("interval") == true){
moveAgent();
}
}
// generate random float value ranging between -0.1 to 0.1.
float generateRandomFloat(){
return(randomNumGenerator.nextFloat() * 0.2f - 0.1f);
}
// move the agent randomly.
void moveAgent(){
agentPosition[0] += generateRandomFloat();
agentPosition[1] += generateRandomFloat();
agentPosition[2] += generateRandomFloat();
// move the agent to the new position.
setAgentPosition.setValue(agentPosition);
}
}